﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using CaseManagerLibrary.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CaseManagerLibrary.Http;
using CaseManagerLibrary.Encrypt;

namespace CaseManagerLibrary.Auth
{
    public static class LoginService
    {
        public static ac_user User { get; set; }
        public static string Token { get; set; }

        public static HResult Login(string address, string userName, string password)
        {
            var result = new HResult();
            byte[] b = System.Text.Encoding.UTF8.GetBytes(password);
            //转成 Base64 形式的 System.String  
            string password_base64 = Convert.ToBase64String(b);

            string url = address + "api/account/login";
            var data = new Dictionary<string, string>();
            data.Add("username", userName);
            data.Add("password", password_base64);
            data.Add("type", "web");
            string response = HttpHelper.PostLogin(url, data, System.Text.Encoding.UTF8);

            JObject obj = JObject.Parse(response);

            result.success = obj["success"].Value<Boolean>();

            if (result.success)
            {
                User = new ac_user()
                {
                    id = obj["userid"].Value<string>(),
                    user_name = userName,
                    real_name = obj["realname"].Value<string>()
                };

                Token = obj["token"].Value<string>();

                Uri uri = new Uri(address);

                HttpHelper.cookies.Add(new System.Net.Cookie("userid" + uri.Port, User.id, "/", uri.Host));
                HttpHelper.cookies.Add(new System.Net.Cookie("token" + uri.Port, Token, "/", uri.Host));
            }
            else
            {
                result.message = obj["message"].Value<string>();
            }


            return result;
        }

        public static HResult LoginRSA(string address, string pemFile, string userName, string passworod)
        {
            var result = new HResult();
            var rsaHelper = new BouncyCastleRSAHelper();
            rsaHelper.ImportPublicKeyPem(pemFile);
            string encrypted = rsaHelper.EncryptText(passworod);
            var loginDto = new { userName = userName, password = encrypted };
            string json = JsonConvert.SerializeObject(loginDto, Formatting.Indented);
            string url = address + "api/account/psdLogin";
            string response = HttpHelper.Post(url, json);

            JObject obj = JObject.Parse(response);

            result.success = obj["success"].Value<Boolean>();

            if (result.success)
            {
                User = new ac_user()
                {
                    id = obj["userid"].Value<string>(),
                    user_name = userName,
                    real_name = obj["realname"].Value<string>()
                };

                Token = obj["token"].Value<string>();

                HttpHelper.headers.Add("token", Token);
            }
            else
            {
                result.message = "用户名或密码不正确"; // obj["message"].Value<string>();
            }

            return result;
        }

        public static HResult LoginOffline(string userName, string password)
        {
            HResult result = new HResult();
            return result;
        }

        public static void Logout()
        {
            User = null;
            HttpHelper.cookies = new System.Net.CookieContainer();
            HttpHelper.headers.Clear();
        }
    }
}
